package com.limeng.datastruts.stack;

public class LinedStack {

    public static void main(String[] args) {

        LinedStack stack = new LinedStack();
        stack.push(4);
        stack.push(5);
        stack.push(1);
        stack.push(6);
        stack.push(3);
        stack.push(2);

        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }

    private Node node;

    public LinedStack() {
        node = new Node();
        node.next = null;
    }

    public boolean push(int num){
        Node n = new Node(num);
        n.next = node;
        node = n;
        return true;
    }

    public int pop(){
        if(node.next == null){
            throw new RuntimeException("已无元素。。。");
        }
        int oldData = node.data;
        node = node.next;
        return oldData;
    }

    class Node{
        int data;
        Node next;

        public Node() {
        }

        public Node(int num) {
            this.data = num;
        }
    }

}
